package com.starbucks.opencase.tool;

import com.starbucks.opencase.modualr.moGoodsSummary;
import com.starbucks.opencase.modualr.moPetInfo;
import com.starbucks.opencase.modualr.moPetSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class toolUserPet {
    @Autowired
    private JdbcTemplate con;
    public List<moPetSummary> queryActive(String uid){
        return con.query("select a.pid,b.`name` from user_pet_bag as a join pet_info as b on a.pid=b.pid where a.uid=? and a.sleep=0",new BeanPropertyRowMapper<>(moPetSummary.class),uid);
    }
    public String queryName(String pid){
        return con.queryForObject("select `name` from pet_info where pid=?", String.class,pid);
    }
    public String randomPetIdForNotHas(String uid){
        try {
            return con.queryForObject("select pid from pet_info where pid not in (select pid from user_pet_bag where uid=?) order by rand() limit 1", String.class, uid);
        }catch (Exception e){
            return "";
        }
    }
    public void add(String uid,String pid){
        con.update("insert into user_pet_bag values(?,?,?)",uid,pid,queryActive(uid).size()>=8?1:0);
    }
    public moPetInfo queryInfo(String pid){
        List<moPetInfo> as=con.query("select pid,name as pname,attack,defence,blood from pet_info where pid=?",new BeanPropertyRowMapper<>(moPetInfo.class),pid);
        if(as.isEmpty())return null;
        return as.get(0);
    }
    public String randomPID(){
        try {
            return con.queryForObject("select pid from pet_info order by rand() limit 1", String.class);
        }catch (Exception e){
            return "";
        }
    }
}
